# -*- coding: utf-8 -*-

from iFinDPy import *
import os
from threading import Lock,Semaphore
import pandas as pd

# 登录函数
def thslogindemo():
    # 输入用户的帐号和密码
    thsLogin = THS_iFinDLogin("xbzqsh123","dc7037")
    print(thsLogin)
    if thsLogin != 0:
        print('登录失败')
    else:
        print('登录成功')

thslogindemo()
#
sem = Semaphore(5)  # 此变量用于控制最大并发数
dllock = Lock()  #此变量用来控制实时行情推送中落数据到本地的锁

# def process_securities_and_dates(secu_file, date_file, output_file, indicators):
#     """
#     从secu_file读取证券代码，从date_file读取交易日期，
#     调用THS_BD函数并将结果写入output_file文件。
#
#     参数:
#     secu_file (str): 证券代码文件路径
#     date_file (str): 交易日期文件路径
#     output_file (str): 输出文件路径
#     indicators (str): 指标值
#
#     返回:
#     None
#     """
#
#     # 读取证券代码和交易日期
#     secu_list = pd.read_csv(secu_file)['secu_code'].tolist()
#     trading_dates = pd.read_csv(date_file)['trading_date'].tolist()
#     # trading_dates = ['2014-01-03', '2024-06-18']
#     # secu_list = ['000002.SZ', '000004.SZ']
#
#     # 分批处理secu_list，确保每批不超过1000个元素
#     batch_size = 1000
#     secu_batches = [secu_list[i:i + batch_size] for i in range(0, len(secu_list), batch_size)]
#
#     # 删除旧的输出文件（如果存在）
#     if os.path.exists(output_file):
#         os.remove(output_file)
#
#     # 标记是否已经写入header
#     header_written = False
#
#     # 循环交易日期和每批证券代码，调用THS_BD函数，并将结果写入output_file文件
#     for ths_date in trading_dates:
#         for batch in secu_batches:
#             down_cmd_secu_list = batch
#             # 日期格式需根据指标的不同做相应修改
#             down_cmd_date = f'{ths_date};{ths_date};{ths_date};{ths_date};{ths_date};{ths_date};{ths_date};{ths_date};{ths_date};{ths_date};{ths_date};{ths_date};{ths_date};{ths_date};{ths_date};{ths_date};{ths_date};{ths_date};{ths_date};{ths_date};{ths_date},{ths_date};{ths_date},{ths_date};{ths_date},{ths_date};{ths_date},100;{ths_date},100;{ths_date},100;{ths_date},100;{ths_date},100;{ths_date},100;{ths_date},100;{ths_date},100;{ths_date},100;{ths_date},100;{ths_date},100;{ths_date};{ths_date};{ths_date};{ths_date};{ths_date};{ths_date};{ths_date};{ths_date};{ths_date};{ths_date};{ths_date};{ths_date};{ths_date};{ths_date};{ths_date};{ths_date};{ths_date};{ths_date};{ths_date};{ths_date};{ths_date};{ths_date};{ths_date};{ths_date};{ths_date};{ths_date};{ths_date};{ths_date};{ths_date};{ths_date};{ths_date};{ths_date};{ths_date};{ths_date};{ths_date};{ths_date};{ths_date};{ths_date};{ths_date};{ths_date};{ths_date};{ths_date};{ths_date};{ths_date};{ths_date};{ths_date};{ths_date};{ths_date};{ths_date};{ths_date},3,100;{ths_date},3,100;{ths_date},3,60,100;{ths_date},3,60,100;{ths_date},60,3,100;{ths_date},60,3,100;{ths_date};{ths_date};{ths_date},5,10,20,30,100,101;{ths_date};{ths_date},3,100;{ths_date},3,100;{ths_date},5,100;{ths_date},6,101,100;{ths_date},5,100;{ths_date},12,26,9,100,100;{ths_date},12,26,100;{ths_date},10,100;{ths_date},26,100,100;{ths_date},12,6,101,100,100;{ths_date},26,100;{ths_date},26,100,100,100;{ths_date},30,101,100,100;{ths_date},14,100,100;{ths_date},100,100,100;{ths_date},100,100;{ths_date},24,6,101,100,100;{ths_date},3,6,12,24,100,100;{ths_date},13,30,10,5,100,100,100;{ths_date},10,50,10,100,100,100;{ths_date},5,100,100;{ths_date},5,100,100;{ths_date},26,12,9,100,100,100;{ths_date},6,6,101,100,100;{ths_date},26,12,100,100;{ths_date},12,20,100,100,100;{ths_date},100;{ths_date},100;{ths_date},12,100,100;{ths_date},14,100,100;{ths_date},5,16,76,100,100,100;{ths_date},20,6,100,100,100;{ths_date},9,3,3,100,100,100;{ths_date},9,3,3,100,100,100;{ths_date},12,6,100,100,100;{ths_date},6,100,100;{ths_date},100,100;{ths_date},30,101,100,100;{ths_date},12,100;{ths_date},6,100;{ths_date},,100,100;{ths_date},1,100,100;{ths_date},23,8,100,100,100;{ths_date},9,3,100,100,100;{ths_date},10,3,100,100,100;{ths_date},26,2,100,100,100;{ths_date},100,100,100;{ths_date},4,2,10,100,100;{ths_date},14,101,100,100;{ths_date},12,100,100,100;{ths_date},7,100,100,100;{ths_date},20,100,100,100;{ths_date},20,100,100,100;{ths_date},125,5,20,100,100,100;{ths_date},12,100,100,100;{ths_date},3,10,20,100,100,100;{ths_date},50,100,100;{ths_date},59,21,28,100,100,100;{ths_date},9,100,100;{ths_date},14,100,100,100;{ths_date},10,100,100;{ths_date},9,25,100,100;{ths_date},26,100,100;{ths_date},28,100,100;104;104;104;104;{ths_date},{ths_date},1;{ths_date},{ths_date},1;{ths_date},{ths_date},OC;104;104;104;2024;2024;2024;;104;2024,100;2024;104;104;{ths_date}'
#
#             # 调用THS_BD函数
#             ths_indicator_result = THS_BD(down_cmd_secu_list, indicators, down_cmd_date)
#             ths_indicator_result.data['trading_date'] = ths_date
#
#             # 将结果追加写入CSV文件
#             ths_indicator_result.data.to_csv(output_file, mode='a', header=not header_written, index=False)
#             header_written = True


def process_securities_and_dates(secu_file, date_file, indicators):
    """
    从secu_file读取证券代码，从date_file读取交易日期，
    调用THS_BD函数并将结果按月保存到不同的CSV文件中。

    参数:
    secu_file (str): 证券代码文件路径
    date_file (str): 交易日期文件路径
    indicators (str): 指标值

    返回:
    None
    """

    # 读取证券代码和交易日期
    secu_list = pd.read_csv(secu_file)['secu_code'].tolist()
    trading_dates = pd.read_csv(date_file)['trading_date'].tolist()

    # trading_dates = ['2014-01-03', '2024-06-18', '2024-06-17']
    # secu_list = ['000002.SZ', '000004.SZ']

    # 将交易日期转换为datetime格式，并按月分组
    trading_dates = pd.to_datetime(trading_dates)
    trading_dates_grouped = trading_dates.to_series().groupby(trading_dates.to_period('M'))

    # 分批处理secu_list，确保每批不超过1000个元素
    batch_size = 1000
    secu_batches = [secu_list[i:i + batch_size] for i in range(0, len(secu_list), batch_size)]

    # 循环每个月
    for period, dates in trading_dates_grouped:
        # 获取当前月份的第一个交易日
        first_date_of_month = period.start_time.strftime('%Y.%m.%d')
        output_file = f'F:\\Personal\\data\\merged\\indicators\\{first_date_of_month}.csv'

        # 删除旧的输出文件（如果存在）
        if os.path.exists(output_file):
            os.remove(output_file)

        # 标记是否已经写入header
        header_written = False

        # 循环当前月份的所有交易日期
        for trading_date in dates:
            trading_date = trading_date.strftime('%Y-%m-%d')
            print(trading_date)
            for batch in secu_batches:
                down_cmd_secu_list = batch
                # 日期格式需根据指标的不同做相应修改
                down_cmd_date = f'{trading_date};{trading_date};{trading_date};{trading_date};{trading_date};{trading_date};{trading_date};{trading_date};{trading_date};{trading_date};{trading_date};{trading_date};{trading_date};{trading_date};{trading_date};{trading_date};{trading_date};{trading_date};{trading_date};{trading_date};{trading_date},{trading_date};{trading_date},{trading_date};{trading_date},{trading_date};{trading_date},100;{trading_date},100;{trading_date},100;{trading_date},100;{trading_date},100;{trading_date},100;{trading_date},100;{trading_date},100;{trading_date},100;{trading_date},100;{trading_date},100;{trading_date};{trading_date};{trading_date};{trading_date};{trading_date};{trading_date};{trading_date};{trading_date};{trading_date};{trading_date};{trading_date};{trading_date};{trading_date};{trading_date};{trading_date};{trading_date};{trading_date};{trading_date};{trading_date};{trading_date};{trading_date};{trading_date};{trading_date};{trading_date};{trading_date};{trading_date};{trading_date};{trading_date};{trading_date};{trading_date};{trading_date};{trading_date};{trading_date};{trading_date};{trading_date};{trading_date};{trading_date};{trading_date};{trading_date};{trading_date};{trading_date};{trading_date};{trading_date};{trading_date};{trading_date};{trading_date};{trading_date};{trading_date};{trading_date};{trading_date},3,100;{trading_date},3,100;{trading_date},3,60,100;{trading_date},3,60,100;{trading_date},60,3,100;{trading_date},60,3,100;{trading_date};{trading_date};{trading_date},5,10,20,30,100,101;{trading_date};{trading_date},3,100;{trading_date},3,100;{trading_date},5,100;{trading_date},6,101,100;{trading_date},5,100;{trading_date},12,26,9,100,100;{trading_date},12,26,100;{trading_date},10,100;{trading_date},26,100,100;{trading_date},12,6,101,100,100;{trading_date},26,100;{trading_date},26,100,100,100;{trading_date},30,101,100,100;{trading_date},14,100,100;{trading_date},100,100,100;{trading_date},100,100;{trading_date},24,6,101,100,100;{trading_date},3,6,12,24,100,100;{trading_date},13,30,10,5,100,100,100;{trading_date},10,50,10,100,100,100;{trading_date},5,100,100;{trading_date},5,100,100;{trading_date},26,12,9,100,100,100;{trading_date},6,6,101,100,100;{trading_date},26,12,100,100;{trading_date},12,20,100,100,100;{trading_date},100;{trading_date},100;{trading_date},12,100,100;{trading_date},14,100,100;{trading_date},5,16,76,100,100,100;{trading_date},20,6,100,100,100;{trading_date},9,3,3,100,100,100;{trading_date},9,3,3,100,100,100;{trading_date},12,6,100,100,100;{trading_date},6,100,100;{trading_date},100,100;{trading_date},30,101,100,100;{trading_date},12,100;{trading_date},6,100;{trading_date},,100,100;{trading_date},1,100,100;{trading_date},23,8,100,100,100;{trading_date},9,3,100,100,100;{trading_date},10,3,100,100,100;{trading_date},26,2,100,100,100;{trading_date},100,100,100;{trading_date},4,2,10,100,100;{trading_date},14,101,100,100;{trading_date},12,100,100,100;{trading_date},7,100,100,100;{trading_date},20,100,100,100;{trading_date},20,100,100,100;{trading_date},125,5,20,100,100,100;{trading_date},12,100,100,100;{trading_date},3,10,20,100,100,100;{trading_date},50,100,100;{trading_date},59,21,28,100,100,100;{trading_date},9,100,100;{trading_date},14,100,100,100;{trading_date},10,100,100;{trading_date},9,25,100,100;{trading_date},26,100,100;{trading_date},28,100,100;104;104;104;104;{trading_date},{trading_date},1;{trading_date},{trading_date},1;{trading_date},{trading_date},OC;104;104;104;2024;2024;2024;;104;2024,100;2024;104;104;{trading_date}'

                # 调用THS_BD函数
                ths_indicator_result = THS_BD(down_cmd_secu_list, indicators, down_cmd_date)
                ths_indicator_result.data['trading_date'] = trading_date

                # 将结果追加写入当月的CSV文件
                ths_indicator_result.data.to_csv(output_file, mode='a', header=not header_written, index=False)
                header_written = True

# 调用封装函数的示例
secu_file = 'full_secu_list.csv'
date_file = 'trading_date.csv'
output_file = 'secu_indicator.csv'

indicators = 'ths_margin_trading_sell_amt_stock;ths_leveraged_buyout_quantity_stock;ths_margin_trading_repay_amt_stock;ths_financing_repay_stock;ths_margin_trading_balance_stock;ths_margin_trading_margin_stock;ths_short_selling_sell_amt_stock;ths_short_selling_sell_vol_stock;ths_short_selling_repay_stock;ths_short_selling_payment_vol_stock;ths_short_selling_amtb_stock;ths_short_selling_vol_balance_stock;ths_margin_trading_amtb_stock;ths_lendable_shares_stock;ths_lending_margin_stock;ths_single_stock_guarantee_ratio_stock;ths_net_purchases_stock;ths_net_buy_amt_ratio_stock;ths_net_sell_amt_stock;ths_net_sell_amt_ratio_stock;ths_abnormal_trade_num_stock;ths_abnormal_trade_ba_stock;ths_abnormal_trade_sa_stock;ths_inflow_amt_stock;ths_outflow_amt_stock;ths_inflow_vol_stock;ths_outflow_vol_stock;ths_net_buy_vol_stock;ths_active_buy_amt_stock;ths_active_sell_amt_stock;ths_active_buy_vol_stock;ths_active_sell_vol_stock;ths_net_active_buy_amt_stock;ths_net_active_buy_vol_stock;ths_active_buy_large_vol_stock;ths_active_buy_large_amt_stock;ths_possitive_buy_large_vol_stock;ths_possitive_buy_large_amt_stock;ths_active_sell_large_vol_stock;ths_active_sell_large_amt_stock;ths_possitive_sell_large_vol_stock;ths_possitive_sell_large_amt_stock;ths_active_buy_main_vol_stock;ths_active_buy_main_amt_stock;ths_possitive_buy_main_vol_stock;ths_possitive_buy_main_amt_stock;ths_active_sell_main_vol_stock;ths_active_sell_main_amt_stock;ths_possitive_sell_main_vol_stock;ths_possitive_sell_main_amt_stock;ths_active_buy_middle_vol_stock;ths_active_buy_middle_amt_stock;ths_possitive_buy_middle_vol_stock;ths_possitive_buy_middle_amt_stock;ths_active_sell_middle_vol_stock;ths_active_sell_middle_amt_stock;ths_possitive_sell_middle_vol_stock;ths_possitive_sell_middle_amt_stock;ths_active_buy_small_vol_stock;ths_active_buy_small_amt_stock;ths_active_sellsmall_vol_stock;ths_active_sellsmall_amt_stock;ths_small_buy_vol_stock;ths_small_buy_amt_stock;ths_small_sell_vol_stock;ths_small_sell_amt_stock;ths_dde_stock;ths_dde_net_vol_stock;ths_net_inflow_amt_stock;ths_net_infow_stock;ths_dde_5d_stock;ths_dde_10d_stock;ths_dde_20d_stock;ths_net_active_buy_amt_rate_stock;ths_net_active_buy_amt_ratio_stock;ths_net_active_buy_vol_rate_stock;ths_net_active_buy_vol_ratio_stock;ths_inflow_rate_stock;ths_main_net_inflow_amt_ratio_stock;ths_main_inflow_rate_stock;ths_main_net_inflow_vol_ratio_stock;ths_up_days_stock;ths_down_days_stock;ths_his_high_stock;ths_his_low_stock;ths_phase_high_stock;ths_phase_low_stock;ths_up_valid_break_ma_stock;ths_down_valid_break_ma_stock;ths_up_open_date_stock;ths_dl_open_date_stock;ths_long_or_short_by_avg_line_stock;ths_maxup_nd_stock;ths_his_high_nearly_days_stock;ths_his_low_nearly_days_stock;ths_vol_ratio_stock;ths_tapi_stock;ths_vma_stock;ths_vmacd_stock;ths_vosc_stock;ths_vstd_stock;ths_cr_stock;ths_psy_stock;ths_vr_stock;ths_arbr_stock;ths_wad_stock;ths_mfi_stock;ths_obv_stock;ths_pvt_stock;ths_wvad_stock;ths_bbi_stock;ths_ddi_stock;ths_dma_stock;ths_ma_stock;ths_expma_stock;ths_macd_stock;ths_mtm_stock;ths_price_osc_stock;ths_trix_stock;ths_ddzbczq_stock;ths_ddzbzzq_stock;ths_bias_stock;ths_cci_stock;ths_dbcd_stock;ths_dpo_stock;ths_kdj_stock;ths_lwr_stock;ths_roc_stock;ths_rsi_stock;ths_si_stock;ths_srdm_stock;ths_vroc_stock;ths_vrsi_stock;ths_wr_stock;ths_b3612_stock;ths_adtm_stock;ths_skdj_stock;ths_bbiboll_stock;ths_boll_stock;ths_cdp_stock;ths_sar_stock;ths_env_stock;ths_mike_stock;ths_dptb_stock;ths_jdqs_stock;ths_jdrs_stock;ths_zdzb_stock;ths_mi_stock;ths_micd_stock;ths_rc_stock;ths_rccd_stock;ths_srmi_stock;ths_atr_stock;ths_cvlt_stock;ths_mass_stock;ths_std_stock;ths_vhf_stock;ths_dividend_benchmark_shares_stock;ths_dividend_ps_stock;ths_dividend_ps_before_tax_stock;ths_dividend_ps_after_tax_stock;ths_dividend_ps_before_tax_int_stock;ths_dividend_ps_after_tax_int_stock;ths_unit_total_cash_dividend_stock;ths_dividend_income_rate_stock;ths_bonus_shares_ps_stock;ths_conversion_increase_shareps_stock;ths_annual_cash_dividend_num_stock;ths_accum_unit_dividend_y_stock;ths_accum_total_dividend_y_stock;ths_accum_total_dividend_y_merge_stock;ths_dividend_ratio_stock;ths_annual_dividend_ratio_stock;ths_accum_dividend_ratio_in3y_stock;ths_cash_dividend_ratio_stock;ths_dividend_rate_from_ipo_stock;ths_latest_dividend_rp_stock'

process_securities_and_dates(secu_file, date_file, indicators)
